package licode.unisop.log.service.infrastructure.repository.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import licode.unisop.log.client.api.AuditLogApi;
import licode.unisop.log.client.dto.AuditLogDTO;
import licode.unisop.log.client.req.AuditLogIn;
import licode.unisop.log.service.infrastructure.assembler.LogAssembler;
import licode.unisop.log.service.infrastructure.repository.mp.mapper.AuditLogMapper;
import licode.unisop.log.service.infrastructure.repository.po.AuditLogPO;
import licode.unisop.types.util.IdGenUtil;
import licode.unisop.unilite.dp.PageData;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author WR
 * @since 2016/7/9 16:05
 */
@Primary
@Component
public class AuditLogApiImpl
        extends ServiceBase<AuditLogMapper, AuditLogPO>
        implements AuditLogApi {
    @Resource
    private LogAssembler logAssembler;

    @Override
    public void save(AuditLogDTO source) {
        AuditLogPO dest = logAssembler.fromAuditLogDto(source);
        dest.setId(IdGenUtil.buildAuditLogId());
        super.save(dest);
    }

    @Override
    public void saveList(List<AuditLogDTO> source) {
        List<AuditLogPO> dest = logAssembler.fromAuditLogDto(source);
        for (AuditLogPO item: dest) {
            item.setId(IdGenUtil.buildAuditLogId());
            super.save(item);
        }
    }

    @Override
    public PageData<AuditLogDTO> query(AuditLogIn req) {
        Page<AuditLogPO> page = new Page<>(req.getCurrent(),
                req.getPageSize());
        page = super.lambdaQuery()
                .eq(StrUtil.isNotBlank(req.getPlatformId()),
                        AuditLogPO::getPlatformId, req.getPlatformId())
                .in(null != req.getPlatformIds() && !req.getPlatformIds().isEmpty(),
                        AuditLogPO::getPlatformId, req.getPlatformIds())
                .like(StrUtil.isNotBlank(req.getClientId()),
                        AuditLogPO::getClientId, req.getClientId())
                .like(StrUtil.isNotBlank(req.getPlatformName()),
                        AuditLogPO::getPlatformName, req.getPlatformName())
                .like(StrUtil.isNotBlank(req.getUserName()),
                        AuditLogPO::getUserName, req.getUserName())
                .like(StrUtil.isNotBlank(req.getUserAccount()),
                        AuditLogPO::getUserAccount, req.getUserAccount())
                .like(StrUtil.isNotBlank(req.getModuleName()),
                        AuditLogPO::getModuleName, req.getModuleName())
                .like(StrUtil.isNotBlank(req.getTerminalId()),
                        AuditLogPO::getTerminalId, req.getTerminalId())
                .like(StrUtil.isNotBlank(req.getClientId()),
                        AuditLogPO::getClientId, req.getClientId())
                .like(StrUtil.isNotBlank(req.getActionData()),
                        AuditLogPO::getActionData, req.getActionData())
                .like(StrUtil.isNotBlank(req.getActionError()),
                        AuditLogPO::getActionError, req.getActionError())
                .like(StrUtil.isNotBlank(req.getActionType()),
                        AuditLogPO::getActionType, req.getActionType())
                .eq(null != req.getUserId(),
                        AuditLogPO::getUserId, req.getUserId())
                .eq(null != req.getActionStatus(),
                        AuditLogPO::getActionStatus, req.getActionStatus())
                .le(null != req.getEndDate(),
                        AuditLogPO::getActionTime, req.getEndDate())
                .ge(null != req.getStartDate(),
                        AuditLogPO::getActionTime, req.getStartDate())
                .orderByDesc(AuditLogPO::getActionTime)
                .page(page);

        return convertPage(page, (List<AuditLogPO> data) ->
                logAssembler.fromAuditLog(data));
    }
}
